package com.jyu.apts.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * access_token是公众号的全局唯一接口调用凭据
 * @author MoCha
 */
@Getter
@Setter
@NoArgsConstructor
public class AccessTokenInfo {
	private String accessToken; // 获取到的凭证
	private long expiresIn; // 凭证有效时间，毫秒
	private long safeExpiresIn; // 安全凭证有效时间，毫秒
	private long safeExpireDurationms = 1000*60; // 安全凭证安全时间，毫秒,处理网络延迟

	public AccessTokenInfo(String accessToken, String expiresIn) {
		this.accessToken = accessToken;
		this.expiresIn = System.currentTimeMillis() + Integer.parseInt(expiresIn) * 1000 ;
		this.safeExpiresIn = this.expiresIn-safeExpireDurationms;
	}
	
	/**
	 * 验证access_token是否安全和过期失效
	 * @return
	 */
	public boolean isSafeExpired() {
		return System.currentTimeMillis() > this.safeExpiresIn;
	}
	
	/**
	 * 验证access_token是否过期失效
	 * @return	如果过期失效则返回true
	 */
	public boolean isExpired() {
		return System.currentTimeMillis() > this.expiresIn;
	}
}
